/*AyflEomXWyqCHfdTxzWda3ExHehXzPX6thlzEnTk9yg=*/
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { createPage, ajax, base, toast, getMultiLang, createPageIcon } from 'nc-lightapp-front';
import { initTemplate, afterEvent, transferButtonClick } from './events';
import { tableId, formId, leftarea, dataSource } from './constants';
import { bodyBeforeEvent } from '../../../public/components/pubUtils/arapTableRefFilter';
import { formBeforeEvent } from '../../../public/components/pubUtils/arapFormRefFilter';
import {cardSocketConnect,cardSocketErrorFlag,toggleBtnStatus} from '../../../public/components/pubUtils/MicroServiceSocket.js';
let arr = [
	'MONEY_DE',
	'LOCAL_MONEY_DE',
	'QUANTITY_DE',
	'MONEY_BAL',
	'LOCAL_MONEY_BAL',
	'QUANTITY_BAL',
	'LOCAL_TAX_DE',
	'NOTAX_DE',
	'LOCAL_NOTAX_DE',
	'GROUPDEBIT',
	'GROUPTAX_DE',
	'GROUPNOTAX_DE',
	'GLOBALDEBIT',
	'GLOBALTAX_DE',
	'GLOBALNOTAX_DE',
	'QUANTITY_CR',
	'LOCAL_MONEY_CR',
	'MONEY_CR',
	'LOCAL_TAX_CR',
	'NOTAX_CR',
	'LOCAL_NOTAX_CR',
	'GROUPCREBIT',
	'GROUPTAX_CRE',
	'GROUPNOTAX_CRE',
	'GLOBALCREBIT',
	'GLOBALTAX_CRE',
	'GLOBALNOTAX_CRE',
	'OCCUPATIONMNY',
	'PRICE',
	'GROUPBALANCE',
	'GLOBALBALANCE',
	'TAXPRICE',
	'CALTAXMNY',
	'NOSUBTAX',
	'MONEY',
	'GROUPLOCAL',
	'GLOBALLOCAL',
	'PK_CURRTYPE',
	'PK_TRADETYPE',
	'PK_TRADETYPEID',
	'PK_BILLTYPE',
	'RATE',
	'GROUPRATE',
	'GLOBALRATE'
];

//大写转小写方法
let ToLowerCase = () => {
	return arr.map((item, index, array) => {
		return item.toLowerCase();
	});
};
let brr = ToLowerCase();
/**
 * 收款单为资金到账通知提供卡片页面
 */
class Card extends Component {
	constructor(props) {
		super(props);
		this.appcode = this.props.getSearchParam('c')
		this.pagecode = this.props.getSearchParam("p");
		this.formId = formId;
		this.tableId = tableId;
		this.leftarea = leftarea;
		this.tradetype = this.props.getUrlParam("billtype")
		if (this.tradetype && (this.tradetype.substring(0, 2) == ("F2") || this.tradetype.substring(0, 2) == ("D2"))) {
			this.billType = "F2"
		} else if (this.tradetype && (this.tradetype.substring(0, 2) == ("F3") || this.tradetype.substring(0, 2) == ("D3"))) {
			this.billType = "F3"
		}
		this.pkName = this.billType == 'F2' ? 'pk_gatherbill' : 'pk_paybill';
		this.pkname= this.billType == 'F2' ? 'pk_gatherbill' : 'pk_paybill';
		this.transferIndex = 0; //转单时左侧选择的数据序号
		this.state = {
			json: {}
		};
		props.use.form(this.formId);
		 props.use.cardTable(this.tableId);
	}

	componentDidMount() {
		let callback = (json) => {
			this.setState({ json: json }, () => {
				initTemplate.call(this, this.props, this.initShow);
			});
		}
		getMultiLang({ moduleId: ['arappub', 'public'], domainName: 'arap', currentLocale: 'simpchn', callback });
	}

	//页面初始化
	initShow = () => {
		if (this.props.getUrlParam('src') === '36S3') {
			let ids = this.props.getUrlParam('pks');
			let source = this.props.getUrlParam('source');
			if (ids) {
				let data = {
					pk_bill: ids,
					pageId: this.pagecode,
					billType: this.tradetype,
					source: source
				};
				ajax({
					url: '/nccloud/arap/arappub/releasetoarap.do',
					data: data,
					success: (res) => {
						if (res && res.data) {
							this.props.transferTable.setTransferListValue(
								this.leftarea,
								res.data
							)
						}
						if (source && source == 'refund') {
							if (res.data) {
								this.props.form.setFormItemsDisabled(this.formId, {
									pk_currtype: true,
									rate: true,
									globalrate: true,
									grouprate: true,
									objtype: true
								});
								setTimeout(() => {
									this.props.cardTable.setColEditableByKey(this.tableId, brr, true);
								}, 500);
							}
						}

						this.toggleShow(res)
					}
				})
			} else {
				this.props.transferTable.setTransferListValue(this.leftarea, []);
			}
		}
	}

	//同步单据信息到转单控件
	synTransferData = () => {
		//重取界面现有数据赋值到转单中
		let amount = this.props.transferTable.getTransformFormAmount(this.leftarea)
		if (amount != 1) {
			let cardData = this.props.createMasterChildData(this.pagecode, this.formId, this.tableId);
			this.props.transferTable.setTransferListValueByIndex(this.leftarea, cardData, this.transferIndex);
		}
	}

	//切换页面状态
	toggleShow = (res) => {
		if(res){
			toggleBtnStatus.call(this,res,'card_head');
		}
		let status = this.props.getUrlParam('status');
		let flag = status === 'browse' ? true : false;
		this.props.button.setButtonVisible(['Edit'], flag);
		this.props.button.setButtonVisible(['Save', 'Cancel', 'Delete_inner'], !flag);
		let source = this.props.getUrlParam('source');
		if (source && source == 'refund') {
			this.props.button.setButtonVisible(['Quit'], false);
			this.props.button.setButtonVisible(['QuitRefund'], true);
		} else {
			this.props.button.setButtonVisible(['Quit'], true);
			this.props.button.setButtonVisible(['QuitRefund'], false);
		}

		this.props.form.setFormStatus(formId, status);
		this.props.cardTable.setStatus(tableId, status);
		if (status == 'add') {
			this.props.cardTable.setStatus(this.tableId, 'edit');
		} else {
			this.props.cardTable.setStatus(this.tableId, status);
		}
		this.props.form.setFormItemsDisabled(this.formId, { 'pk_org': true });
	}

	//获取列表肩部信息
	getTableHead = (buttons) => {
		return (
			<span>
				{this.props.button.createButtonApp({
					area: 'card_body',
					buttonLimit: 3,
					// onButtonClick: buttonClick.bind(this),
					popContainer: document.querySelector('.header-button-area')
				})}
			</span>
		)
	}


	render() {
		let { cardTable, form, button, modal, transferTable } = this.props;
		let buttons = this.props.button.getButtons();
		let { createForm } = form;
		let { createCardTable } = cardTable;
		let { createButton } = button;
		let { createModal } = modal;
		const { createTransferList } = transferTable;
		return (
			<div id="transferCard" className="nc-bill-transferList">
				<div className="nc-bill-header-area">
					<div className="header-title-search-area">
						{createPageIcon()}
						<h2 className="title-search-detail">{this.billType == 'F2' ? this.state.json['arappub-000003'] : this.state.json['arappub-000004']}</h2>
					</div>
					<div className="header-button-area">
					    {cardSocketConnect.call(this,'card_head',dataSource)}
						{cardSocketErrorFlag.call(this,'card_head')}
						{this.props.button.createButtonApp({
							area: 'card_head',
							buttonLimit: 3,
							onButtonClick: transferButtonClick.bind(this),
							popContainer: document.querySelector('.header-button-area')
						})}
					</div>
				</div>
				<div className="nc-bill-transferList-content">
					{createTransferList({
						headcode: this.formId,
						bodycode: this.tableId,
						transferListId: this.leftarea,
						onTransferItemSelected: (record, status, index) => {
							//转单缩略图被选中时的钩子函数
							this.transferIndex = index;
							let isEdit = status ? 'browse' : 'edit';
							if (isEdit == 'browse') {
								this.props.setUrlParam({ status: 'browse' })
								let id = record.head[this.formId].rows[0].values[this.pkName].value
								this.props.setUrlParam({ id: id })
							} else {
								this.props.setUrlParam({ status: 'add' })
								this.props.delUrlParam('id')
							}
							this.toggleShow(record.head[this.formId]);
							this.props.form.setAllFormValue({ [this.formId]: record.head[this.formId] });
							this.props.cardTable.setTableData(this.tableId, record.body[this.tableId]);
							this.props.form.setFormStatus(this.formId, isEdit);
							this.props.cardTable.setStatus(this.tableId, isEdit);
						}
					})}
					<div className="transferList-content-right nc-bill-card" >
						<div className="nc-bill-form-area">
							{createForm(this.formId, {
								fieldid: "forcmp36S3",
								onAfterEvent: afterEvent.bind(this),
								onBeforeEvent: formBeforeEvent.bind(this)
							})}
						</div>
						<div className="nc-bill-table-area">
							{createCardTable(this.tableId, {
								fieldid: "forcmp36S3",
								tableHead: this.getTableHead.bind(this, buttons),
								modelSave: this.saveBill,
								onAfterEvent: afterEvent.bind(this),
								onBeforeEvent: bodyBeforeEvent.bind(this),
								showCheck: true,
								showIndex: true,
								isAddRow: false,
								adaptionHeight: true
							})}
						</div>
					</div>
				</div>
			</div>
		)
	}
}

Card = createPage({
	orderOfHotKey: [formId, tableId],//区域快捷键切换支持
})(Card);

ReactDOM.render(<Card />, document.querySelector('#app'));

/*AyflEomXWyqCHfdTxzWda3ExHehXzPX6thlzEnTk9yg=*/